package com.yeexun.controller;

import com.yeexun.domain.User;
import com.yeexun.service.UserService;
import com.yeexun.util.Result;
import com.yeexun.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author rookie
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Result login(String code, String imageUrl, String nickName){
        try {
            return userService.login(code, imageUrl, nickName);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("登录失败");
        }
    }


    /**
     * 获取验证码
     * @param phone
     * @return
     */
    @RequestMapping("/phoneVerification")
    public Result verification(String phone){
        try {
            Result verification = userService.verification(phone);
            return verification;
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("失败了");
        }
    }


    /**
     * 绑定手机号
     * @param userId
     * @param phone
     * @return
     */
    @RequestMapping("/bindingPhone")
    public Result bindingPhone(Integer userId,String phone){
        try {
            User user = userService.bindingPhone(userId, phone);

            return ResultGenerator.genSuccessResult(user);
        }catch (Exception e){
            e.printStackTrace();
            return ResultGenerator.genFailResult("绑定失败");
        }
    }
}
